﻿using CardMonitor.Model;
using CardMonitor.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CardMonitor.Windows
{
    /// <summary>
    /// MakeCardWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ReadCardWindow : Window
    {
        SynchronizationContext _syncContext = null;
        public BillModel billModel;
        public String planNum = "";
        public ReadCardWindow()
        {

            InitializeComponent();
            _syncContext = SynchronizationContext.Current;

            billModel = new BillModel();

            this.Loaded += Winndow_Loaded;
            this.Unloaded += Window_UnLoaded;
        }
        public void setModel(BillModel billModel)
        {
            this.billModel = billModel;
        }
        private void Winndow_Loaded(object sender, RoutedEventArgs e)
        {
            App.getGlobalVariables().cardComm.OpenPort(App.getConfig().IC_Card_PortName, 9600, 8, 1);
            Thread.Sleep(100);
            if (App.getGlobalVariables().cardComm.PortState)
            {
                App.getGlobalVariables().cardComm.OnComReceiveDataHandler += Com_DataReceived; //串口接收数据委托
            }
            new Thread(() =>
            {
                //写卡
                try
                {
                    App.getGlobalVariables().cardComm.SendHexData("FF CC 01 58 38 31 30 36 31 38 93");
                    Thread.Sleep(500);
                    App.getGlobalVariables().cardComm.SendHexData("FF CC 01 59 38 31 30 36 31 38 92");
                    Thread.Sleep(500);
                    App.getGlobalVariables().cardComm.SendHexData("FF CC 01 57 38 31 30 36 31 38 9C");
                    Thread.Sleep(500);

                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    _syncContext.Post(CloseWindow, "error");
                    return;
                }
                _syncContext.Post(CloseWindow, "success");
            }).Start();
        }
        private void Window_UnLoaded(object sender, RoutedEventArgs e)
        {
            if (App.getGlobalVariables().cardComm.PortState)
            {
                App.getGlobalVariables().cardComm.ClosePort();
                App.getGlobalVariables().cardComm.OnComReceiveDataHandler -= Com_DataReceived;//串口接收数据委托  
            }
        }
        private void CloseWindow(object text)
        {
            string status = (string)text;
            if ("success".Equals(status))
            {
                this.DialogResult = true;
            }
            this.Close();
        }

        private void Com_DataReceived(String response)
        {
            String[] resArray = ByteUtils.MySplit(response, 2);
            if (resArray.Length < 5) return;

            if (resArray[3] == "68")
            {
                //读GUID 32位
                if (resArray.Length < 37) return;

                String str = "";

                ASCIIEncoding ASCIITochar = new ASCIIEncoding();
                String s = "";
                for (int i = 4; i < 36; i++)
                {
                    s += resArray[i];
                }
                byte[] aa = ByteUtils.strToToHexByte(s);
                char[] ascii = ASCIITochar.GetChars(aa);

                for (int i = 0; i < ascii.Length; i++)
                {
                    str += ascii[i];
                }
                _syncContext.Post(SetGuid, str);
            }
            else if (resArray[3] == "69")
            {
                if (resArray.Length < 21) return;

                ASCIIEncoding ASCIITochar = new ASCIIEncoding();


                //油品代码
                byte[] byteProdCode = ByteUtils.strToToHexByte(resArray[13] + resArray[14]);
                char[] prodCodeAscii = ASCIITochar.GetChars(byteProdCode);
                String strProdCode = "";
                for (int i = 0; i < prodCodeAscii.Length; i++)
                {
                    strProdCode += prodCodeAscii[i];
                }
                _syncContext.Post(SetProdCodeText, strProdCode);
                //预装量
                byte[] byteNums = ByteUtils.strToToHexByte(resArray[15] + resArray[16] + resArray[17] + resArray[18] + resArray[19]);
                char[] ascii = ASCIITochar.GetChars(byteNums);
                String strNum = "";
                for (int i = 0; i < ascii.Length; i++)
                {
                    strNum += ascii[i];
                }
                _syncContext.Post(SetPlanNumText, strNum);
                //车号
                string provinceCode = resArray[4] + resArray[5] + resArray[6];
                string province = CardUtil.decodeProvince(provinceCode);

                byte[] byteCarNum = ByteUtils.strToToHexByte(resArray[7] + resArray[8] + resArray[9] + resArray[10] + resArray[11] + resArray[12]);
                char[] carNumAscii = ASCIITochar.GetChars(byteCarNum);
                String carNumCode = "";
                for (int i = 0; i < carNumAscii.Length; i++)
                {
                    carNumCode += carNumAscii[i];
                }
                carNumCode = province + carNumCode;
                _syncContext.Post(SetCarNumText, carNumCode);
            }

            else if (resArray[3] == "67")
            {
                if (resArray.Length < 13) return;

                ASCIIEncoding ASCIITochar = new ASCIIEncoding();

                //发货单号
                byte[] byteCodes = ByteUtils.strToToHexByte(resArray[4] + resArray[5] + resArray[6] + resArray[7] + resArray[8] + resArray[9] + resArray[10] + resArray[11]);
                char[] ascii = ASCIITochar.GetChars(byteCodes);
                String strNum = "";
                for (int i = 0; i < ascii.Length; i++)
                {
                    strNum += ascii[i];
                }
                _syncContext.Post(SetBillCodeText, strNum);
            }
        }
        private void SetProdCodeText(object obj)
        {
            String msg = (String)obj;
            billModel.ProdCode = msg;
        }

        private void SetBillCodeText(object obj)
        {
            String msg = (String)obj;
            billModel.billCode = msg;
        }
        private void SetGuid(Object obj)
        {
            String msg = (String)obj;
            billModel.FacId = msg;
        }
        private void SetCarNumText(Object obj)
        {
            String msg = (String)obj;
           billModel.CarNumber = msg;
        }
        private void SetPlanNumText(Object obj)
        {
            this.planNum = (string)obj;
        }
    }
}
